package resource;

import gui.frames.Frame;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.HashMap;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

/*
 * @author err0neous
 */
public class ResourceTree {

    private HashMap<String, Object> nodes;
    private String name;

    public ResourceTree() {
        this("root");
    }

    private ResourceTree(String name) {
        nodes = new HashMap<String, Object>();
        this.name = name;
    }

    public ResourceTree node(String sub) {
        if (sub.split("|").length <= 1) {
            nodes.put(sub, new ResourceTree(sub));
            return (ResourceTree) nodes.get(sub);
        } else {
            String down = sub.substring(sub.split("|")[0].length() + 1);
            ResourceTree next = getSubNode(down);
            if (next == null) {
                return null;
            }
            return next.node(down);
        }
    }

    private ResourceTree getSubNode(String sub) {
        Object o = nodes.get(sub.split("|")[0]);
        if (o == null || !(o instanceof ResourceTree)) {
            return null;
        }
        return (ResourceTree) o;
    }

    private Object findRes(String path) {
        if (path.split("|").length <= 1) {
            return nodes.get(path);
        }
        String down = path.substring(path.split("|")[0].length() + 1);
        return getSubNode(down).findRes(down);
    }

    public void putRes(String path, File res) {
        Object o = nodes.get(path.split("|")[0]);
        if (o == null || !(o instanceof ResourceTree)) {
            return;
        }
        if (path.split("|").length <= 2) {
            Object put = res;
            String type = ext(res);
            try {
                if (type.equals("gif")) {
                    put = new ImageIcon(res.toURI().toURL());
                } else if (type.equals("png") || type.equals("jpg")) {
                    put = ImageIO.read(res);
                }
            } catch (MalformedURLException ex) {
                Frame.error(ex);
            } catch (IOException ex) {
                Frame.error(ex);
            }
            ((ResourceTree) o).nodes.put(path.split("|")[1], put);
        } else {
            ((ResourceTree) o).putRes(path.substring(path.split("|")[0].length()), res);
        }
    }

    private String ext(File f) {
        int dot = -1;
        for (int i = f.getName().length() - 1; i >= 0; i--) {
            if (f.getName().charAt(i) == '.') {
                dot = i;
                break;
            }
        }
        return f.getName().substring(dot + 1).toLowerCase();
    }

    public int fill() {
        return fill(0, new File(""));
    }

    private int fill(int start, File pos) {
        File[] files = pos.listFiles(new FilenameFilter() {
            public boolean accept(File dir, String name) {
                return !dir.getPath().equals(new File("").getPath());
            }
        });
        if(files == null)
            return start;
        JOptionPane.showMessageDialog(null, files.length);
        for (File f : files) {
            if (f.isDirectory()) {
                JOptionPane.showMessageDialog(null, f.getPath());
                node(f.getPath().replace('/', '|'));
                start = fill(start, f);
            } else {
                putRes(f.getPath().replace('/', '|'), f);
                start++;
            }
        }
        return start;
    }

    public BufferedImage getBuffered(String path) {
        Object get = findRes(path);
        if (get == null) {
            return null;
        }
        return (BufferedImage) get;
    }

    public ImageIcon getImageIcon(String path) {
        Object get = findRes(path);
        if (get == null) {
            return null;
        }
        return (ImageIcon) get;
    }

    public File getFile(String path) {
        Object get = findRes(path);
        if (get == null) {
            return null;
        }
        return (File) get;
    }

    public boolean equals(Object o) {
        return o instanceof ResourceTree && ((ResourceTree) o).getName().equals(name);
    }

    public String getName() {
        return name;
    }
}
